package com.rongkang.interview.juc.deadLock;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @author: 荣康
 * @date: 2021/12/14
 * <p>
 * 这里是死锁
 */
class myThread implements Runnable {
    private String lockA;
    private String lockB;

    public myThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA) {
            System.out.println(Thread.currentThread().getName() + "\t 线程持有" + lockA + "\t 等待" + lockB);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "\t 线程持有" + lockB + "\t 等待" + lockA);
            }
        }
    }
}

public class DeadLockDemo2 {
    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";
        new Thread(new myThread(lockA, lockB), "threadAAA").start();
        new Thread(new myThread(lockB, lockA), "threadBBB").start();
    }
}
